/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.practica.modelo.dao;


import com.practica.entidad.Parentesco;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Usuario23
 */
public class parentescoDAO {
        private EntityManagerFactory emf ;
        private EntityManager em;

    public parentescoDAO() {
        emf = Persistence.createEntityManagerFactory("TrabajoGrupo2PU");
        em=emf.createEntityManager();
        
       
    }
        
    public boolean insertarParentesco(Parentesco t)
    {
        boolean b= false;
        
        try{
            em.getTransaction().begin();
            em.persist(t);
            em.getTransaction().commit();
            b=true;
            
        } catch(Exception e){
            em.getTransaction().rollback();
        }
         return b;
         
    }
    
       public String  EliminarParentesco(String codigo){
          String b= "0";
       
        try{
            em.getTransaction().begin();
          
        Parentesco t= em.find(Parentesco.class, codigo);
        em.remove(t);
             em.getTransaction().commit(); 
          b="1";
             
        } catch(Exception e){
            //em.getTransaction().rollback();
            b="2";
        }
         return b;
         
    }
    
    
    
    public Parentesco buscarParentesco(String codigo){
          
        Parentesco t= em.find(Parentesco.class, codigo);
        return t;
                
    }
    public List<Parentesco> getListaParentesco(){
        Query q= em.createQuery("Select t from Parentesco t");//jpql
        return q.getResultList();
    }
    
  
    public void closeEntityManager(){
    em.close();
}
}
